using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using System;

public abstract class Sugar : MonoBehaviour 
{
	public ColorSugar colorCd;
	[HideInInspector]
	public int col;
	[HideInInspector]
	public int row;
	[HideInInspector]
	public Sugar[,] arrSugar;
	[HideInInspector]
	public Vector3 posStartSugar;
	[HideInInspector]
	public float sugarSize = 0;
	[HideInInspector]
	public MapLoad mapLoad;
	[HideInInspector]
	public SugarStatus stt = SugarStatus.Transparent;
	public int level = 1;

	[HideInInspector]
	public event Action <Sugar> RegisterFactoryRun;

	Tweener mytween;
	public bool needMove = false;
	// Use this for initialization
	void Start () 
	
	{
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		
	}

	internal void SetIndex(int _col, int _row)
	{
		if (arrSugar != null)
		{
			arrSugar[_col, _row] = this;
			col = _col;
			row = _row;
			needMove = true;			
		}
	}

	public void MoveMySugar()
	{
		if (needMove && stt != SugarStatus.Transparent)
		{
			float speed = DUtil.speedMoveSG;
			Vector3 destina = posStartSugar + new Vector3(col * sugarSize, -row * sugarSize, 0);
			float lenght = (gameObject.transform.position - destina).magnitude;
			float timeMove = lenght / speed;
			if (timeMove == 0 || timeMove >DUtil.minTimeAppear)
			{
				timeMove = DUtil.minTimeAppear;
			}
			if (mytween != null)
			{
				mytween.Complete();
				mytween.Kill();
			}
			mytween = HOTween.To(this.transform, timeMove, new TweenParms().Prop("position", destina).OnComplete(OnCompleteMove));
			needMove = false;
		}
	}

	void OnCompleteMove()
	{
		if (RegisterFactoryRun!=null)
		{
			RegisterFactoryRun(this);
		}
	}

	public void MoveToStartPos()
	{
		gameObject.transform.position = posStartSugar + new Vector3(col * sugarSize, (-row + mapLoad.row) * sugarSize, 0);
	}

	public abstract List<Sugar> GetListMixSugar();
	public abstract void MixWith(Sugar sugar);
	public abstract void RefreshSugar();
	public abstract void LogicMinus();
	public abstract void Logic_Plus();

	void OnClick()
	{
		Debug.Log("col " + col + " row " + row);
	}
}
